package com.mall.order.services;

import com.mall.order.OrderEndService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.constants.OrderItemConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import com.mall.order.utils.ExceptionProcessorUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Component
@Service
public class OrderEndServiceImpl implements OrderEndService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    StockMapper stockMapper;

    @Override
    public DeleteOrderResponse deleteOrder(DeleteOrderRequest deleteOrderRequest) {
        DeleteOrderResponse deleteOrderResponse = new DeleteOrderResponse();
        try {
            deleteOrderRequest.requestCheck();
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(deleteOrderResponse, e);
            return deleteOrderResponse;
        }
        try {
            Order order = orderMapper.selectByPrimaryKey(deleteOrderRequest.getOrderId());
            if (order.getStatus() > 3) {
                //删除order表对应数据
                int affectedRowsNum = orderMapper.deleteByPrimaryKey(deleteOrderRequest.getOrderId());
                if (affectedRowsNum == 1) {
                    //删除order_item表对应的数据
                    Example orderItemExample = new Example(OrderItem.class);
                    Example.Criteria criteria = orderItemExample.createCriteria();
                    criteria.andEqualTo("orderId", deleteOrderRequest.getOrderId());
                    int affectOrderItemNum = orderItemMapper.deleteByExample(orderItemExample);
                    if (affectOrderItemNum == 1) {
                        deleteOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
                        deleteOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
                    }
                }
            } else{
                deleteOrderResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
                deleteOrderResponse.setMsg("未完成订单不可删除");
                return deleteOrderResponse;
            }
        } catch (Exception e) {
            deleteOrderResponse.setCode(OrderRetCode.PIPELINE_RUN_EXCEPTION.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.PIPELINE_RUN_EXCEPTION.getMessage());
            e.printStackTrace();
        }
        return deleteOrderResponse;
    }

    @Override
    public CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest) {
        CancelOrderResponse cancelOrderResponse = new CancelOrderResponse();
        Example orderItemExample = new Example(OrderItem.class);
        Example.Criteria orderItemCriteria = orderItemExample.createCriteria();
        Order order = new Order();
        Stock stock = new Stock();
        try {
            cancelOrderRequest.requestCheck();
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(cancelOrderResponse, e);
            return cancelOrderResponse;
        }

        try {
            //修改商品库存
            orderItemCriteria.andEqualTo("orderId", cancelOrderRequest.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            for (OrderItem orderItem : orderItems) {
                Order order1 = orderMapper.selectByPrimaryKey(orderItem.getOrderId());
                Long itemId = orderItem.getItemId();
                System.out.println(orderItem.getItemId());
                Stock stock1 = stockMapper.selectByPrimaryKey(orderItem.getItemId());
                if (OrderConstants.ORDER_STATUS_INIT == order1.getStatus()) {
                    stock1.setLockCount(-orderItem.getNum());
                    stock1.setStockCount(orderItem.getNum().longValue());
                } else {
                    stock1.setStockCount(orderItem.getNum().longValue());
                }
                stockMapper.updateStock(stock1);
            }
            //修改订单商品锁定状态
            for (OrderItem orderItem : orderItems) {
                orderItem.setId(orderItem.getId());
                orderItem.setStatus(Integer.valueOf(OrderItemConstants.INVENTORY_FREED.getCode()));
                orderItem.setUpdateTime(new Date());
                orderItemMapper.updateByPrimaryKeySelective(orderItem);
            }

        } catch (Exception e) {
            cancelOrderResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
            cancelOrderResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            e.printStackTrace();
        }

        order.setOrderId(cancelOrderRequest.getOrderId());
        order.setStatus(OrderConstants.ORDER_STATUS_TRANSACTION_CANCEL);
        order.setUpdateTime(new Date());
        order.setCloseTime(new Date());
        int updateRows = orderMapper.updateByPrimaryKeySelective(order);
        cancelOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
        cancelOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return cancelOrderResponse;
    }
}
